-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const simpleSelects = `const result = await db | ||
// Create a CTE called \`jennifers\` that selects all | ||
// persons named 'Jennifer'. | ||
.with('jennifers', (db) => db | ||
.selectFrom('person') | ||
.where('first_name', '=', 'Jennifer') | ||
.select(['id', 'age']) | ||
) | ||
// Select all rows from the \`jennifers\` CTE and | ||
// further filter it. | ||
.with('adult_jennifers', (db) => db | ||
.selectFrom('jennifers') | ||
.where('age', '>', 18) | ||
.select(['id', 'age']) | ||
) | ||
// Finally select all adult jennifers that are | ||
// also younger than 60. | ||
.selectFrom('adult_jennifers') | ||
.where('age', '<', 60) | ||
.selectAll() | ||
.execute()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: 'Simple selects' | ||
--- | ||
|
||
# Simple selects | ||
|
||
Common table expressions (CTE) are a great way to modularize complex queries. | ||
Essentially they allow you to run multiple separate queries within a | ||
single roundtrip to the DB. | ||
|
||
Since CTEs are a part of the main query, query optimizers inside DB | ||
engines are able to optimize the overall query. For example, postgres | ||
is able to inline the CTEs inside the using queries if it decides it's | ||
faster. | ||
|
||
import { | ||
Playground, | ||
exampleSetup, | ||
} from '../../../src/components/Playground' | ||
|
||
import { | ||
simpleSelects | ||
} from './0010-simple-selects' | ||
|
||
<div style={{ marginBottom: '1em' }}> | ||
<Playground code={simpleSelects} setupCode={exampleSetup} /> | ||
</div> |
28 changes: 28 additions & 0 deletions
28
site/docs/examples/cte/0020-inserts-updates-and-deletions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export const insertsUpdatesAndDeletions = `const result = await db | ||
.with('new_person', (db) => db | ||
.insertInto('person') | ||
.values({ | ||
first_name: 'Jennifer', | ||
age: 35, | ||
}) | ||
.returning('id') | ||
) | ||
.with('new_pet', (db) => db | ||
.insertInto('pet') | ||
.values({ | ||
name: 'Doggo', | ||
species: 'dog', | ||
is_favorite: true, | ||
// Use the id of the person we just inserted. | ||
owner_id: db | ||
.selectFrom('new_person') | ||
.select('id') | ||
}) | ||
.returning('id') | ||
) | ||
.selectFrom(['new_person', 'new_pet']) | ||
.select([ | ||
'new_person.id as person_id', | ||
'new_pet.id as pet_id' | ||
]) | ||
.execute()` |
21 changes: 21 additions & 0 deletions
21
site/docs/examples/cte/0020-inserts-updates-and-deletions.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: 'Inserts, updates and deletions' | ||
--- | ||
|
||
# Inserts, updates and deletions | ||
|
||
Some databases like postgres also allow you to run other queries than selects | ||
in CTEs. On these databases CTEs are extremely powerful: | ||
|
||
import { | ||
Playground, | ||
exampleSetup, | ||
} from '../../../src/components/Playground' | ||
|
||
import { | ||
insertsUpdatesAndDeletions | ||
} from './0020-inserts-updates-and-deletions' | ||
|
||
<div style={{ marginBottom: '1em' }}> | ||
<Playground code={insertsUpdatesAndDeletions} setupCode={exampleSetup} /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"label": "CTE", | ||
"position": 7, | ||
"link": { | ||
"type": "generated-index", | ||
"description": "Short and simple examples of how to use common table expressions (CTE) in queries." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters